package com.xlx.openLink.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xlx.openLink.base.BasePage;
import com.xlx.openLink.base.BaseUtils;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.dao.ConditionsMapper;
import com.xlx.openLink.dao.DeviceMapper;
import com.xlx.openLink.dao.PortMapper;
import com.xlx.openLink.entity.pojo.Conditions;
import com.xlx.openLink.entity.pojo.Device;
import com.xlx.openLink.entity.pojo.Port;
import com.xlx.openLink.enums.ConType;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.template.OpenLinkTemplate;
import com.xlx.openLink.uitls.AssertUtils;
import com.xlx.openLink.vo.RequestConditionList;
import com.xlx.openLink.vo.RequestDevtLst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Condition;

@Service
public class ConditionService {

    @Autowired
    private ConditionsMapper conditionsMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private PortMapper portMapper;
    @Autowired
    private OpenLinkTemplate openLinkTemplate;

    /**
     * 创建 条件
     *
     * @param condition
     * @return
     */
    @Transactional
    public void addCondition(Conditions condition) {
        Date nDate = new Date();
        condition.setPkGuid(BaseUtils.createGUID());
        condition.setConCreateDate(nDate);
        condition.setConUpdateDate(nDate);
        condition.setConStatus("T");
        if (ConType.DEV.getCode().equalsIgnoreCase(condition.getConType())) {
            Device device = deviceMapper.selectByPrimaryKey(condition.getConDevId());
            AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "设备不存在"), device);
            device.setDevConId(condition.getPkGuid());
            deviceMapper.updateByPrimaryKey(device);
        } else if (ConType.PORT.getCode().equalsIgnoreCase(condition.getConType())) {
            Port port = portMapper.selectByPrimaryKey(condition.getConPortId());
            AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_EMPTY, "数据库不存在数据"), port);
            port.setpConId(condition.getPkGuid());
            portMapper.updateByPrimaryKey(port);
        }
        conditionsMapper.insertSelective(condition);

    }

    /**
     * 更新
     *
     * @param condition
     * @return
     */
    @Transactional
    public int updateCondition(Conditions condition) {
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_EMPTY, "请求参数为空"), condition);
        Date nDate = new Date();
        condition.setConUpdateDate(nDate);
        return conditionsMapper.updateByPrimaryKeySelective(condition);
    }

    /**
     * 删除
     *
     * @param pkGuid
     * @return
     */
    @Transactional
    public void delCondition(String pkGuid) {
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK), pkGuid);
        Conditions condition = conditionsMapper.selectByPrimaryKey(pkGuid);
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_EMPTY, "数据库不存在数据"), condition);
        if (ConType.DEV.getCode().equalsIgnoreCase(condition.getConType())) {
            Device device = deviceMapper.selectByPrimaryKey(condition.getConDevId());
            AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_EMPTY, "数据库不存在数据"), device);
            device.setDevConId(null);
            deviceMapper.updateByPrimaryKey(device);
        } else if (ConType.PORT.getCode().equalsIgnoreCase(condition.getConType())) {
            Port port = portMapper.selectByPrimaryKey(condition.getConPortId());
            AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_EMPTY, "数据库不存在数据"), port);
            port.setpConId(null);
            portMapper.updateByPrimaryKey(port);
        }
        conditionsMapper.deleteByPrimaryKey(pkGuid);
    }

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    public BasePage<Conditions> listByPage(RequestConditionList request) {
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK), request);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Conditions> list = conditionsMapper.listByPage(request);
        PageInfo pageInfo = new PageInfo(list);
        return new BasePage<Conditions>(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }

    @Transactional
    public Conditions select(String pkGuid) {
        return conditionsMapper.selectByPrimaryKey(pkGuid);

    }

    @Transactional
    public Conditions selectGE() {
        return conditionsMapper.selectByGE();

    }

}
